package com.wdz.utils.tenxunyuncos;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.*;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * cos业务类
 */
public class COSService {

    /**
     *
     * @param bucketName 存储桶名
     * @param prefix 查询的文件夹的名字
     */
    public static List<Map<String, Object>> getFiels(String regionName,String bucketName, String prefix){
        COSClient cosClient = COSClientUtil.getCOSClient(regionName);
        // Bucket的命名格式为 BucketName-APPID ，此处填写的存储桶名称必须为此格式
        //bucketName = "wdz-1258450135";
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
        // 设置bucket名称
        listObjectsRequest.setBucketName(bucketName);
        // prefix表示列出的object的key以prefix开始
        //prefix = "";
        listObjectsRequest.setPrefix(prefix);
        // deliter表示分隔符, 设置为/表示列出当前目录下的object, 设置为空表示列出所有的object
        listObjectsRequest.setDelimiter("/");
        // 设置最大遍历出多少个对象, 一次listobject最大支持1000
        listObjectsRequest.setMaxKeys(1000);
        ObjectListing objectListing = null;
        //创建接受文件对象的容器
        List<Map<String,Object>> list = new ArrayList<>();
        try {
            do {
                objectListing = cosClient.listObjects(listObjectsRequest);
                // common prefix表示表示被delimiter截断的路径, 如delimter设置为/, common prefix则表示所有子目录的路径
                List<String> commonPrefixs = objectListing.getCommonPrefixes();
                // object summary表示所有列出的object列表
                List<COSObjectSummary> cosObjectSummaries = objectListing.getObjectSummaries();
                //创建接受文件的子容器
                Map<String,Object> map = null;
                for (COSObjectSummary cosObjectSummary : cosObjectSummaries) {
                    map = new HashMap<>();
                    //存储整个对象
                    map.put("cosObjectSummary",cosObjectSummary);
                    // 文件的路径key
                    map.put("key",cosObjectSummary.getKey());
                    // 文件的etag
                    map.put("etag",cosObjectSummary.getETag());
                    // 文件的长度
                    map.put("fileSize",cosObjectSummary.getSize());
                    // 文件的存储类型
                    map.put("storageClasses",cosObjectSummary.getStorageClass());
                    list.add(map);
                }
                String nextMarker = objectListing.getNextMarker();
                listObjectsRequest.setMarker(nextMarker);
            } while (objectListing.isTruncated());
         } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    } finally {
            cosClient.shutdown();
        }
        return list;
    }

    /**
     *
     * @param localFile 文件
     * @param bucketName 存储桶
     * @param key 文件名（存储桶上的对象键)
     */
    public static void upload(String regionName,File localFile,String bucketName,String key){
        COSClient cosClient = COSClientUtil.getCOSClient(regionName);
        // 指定要上传的文件
        /*File localFile = new File(localFilePath);*/
        // 指定要上传到的存储桶
        // 指定要上传到 COS 上对象键
        //String key = "exampleobject";
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        cosClient.shutdown();
    }

    public static void main(String[] args) {
        //System.out.println(getFiels("wdz-1258450135","/headphotos/"));
        /*File file = new File("C:/Users/Administrator/Pictures/gsl.jpg");
        upload(file,"wdz-1258450135","/test/test.jpg");*/
        String bn = "wdz-1258450135";
        String regionName = "ap-shanghai";
        COSService cosService = new COSService();
        List<Map<String, Object>> headphotoss = cosService.getFiels(regionName,bn,"/headphotos/");
        List<String> headphotos = new ArrayList<>();
        headphotoss.forEach(item->{
            if((long)item.get("fileSize") != 0) {
                headphotos.add(bn + "/" + item.get("key").toString().substring(item.get("key").toString().indexOf("/")));
                System.out.println(bn + "/" + item.get("key").toString().substring(item.get("key").toString().indexOf("/")));
            }
            });
    }
}
